import {useState, useEffect} from 'react'
import axios from 'axios';

const useURLLoader = (url) =>{
    const [data, setData] = useState(null);
    const [loading, setLoading] = useState(false);

    useEffect(() => {
        setLoading(true);
        axios.get(url).then(result => {
            console.log(result);
            setData(result.data);
            setLoading(false);
        }).catch((error) => {
            console.log(error.message);
            setData({
                code: '-1',
                url: 'https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=51f93acdd3c8a786aa27425c0660a258/03087bf40ad162d98635f82b13dfa9ec8a13cd2a.jpg'
            });
            setLoading(false);
        })
    }, [url]);

    return [data, loading]
};

export default useURLLoader;

